/* ***********************
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is SiteAuthorsEmail.js module.
 *
 * The Initial Developer of the Original Code is Rob Gerns.
 *
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 **************************/

/*************************************************
* Function Name(s): SiteAuthorsEmail; writeAuthorsEmail
*
* Purpose:
* Check to see if there are any authors' e-mail addresses
* defined in the 'brief' section of the Oister file.  If 
* so, for each e-mail address create the necessary div and 
* write the author e-mail address inside the div.
**************************************************/

function siteAuthorsEmail(oisterFile){	
	
 //set the author value to blank so that it doesn't contain previously defined value
 var checkSiteAuthors="";

 //check to see if the authors element is defined in the Oister XML
 if(oisterFile.getElementsByTagName("authors")!=undefined){
  checkSiteAuthors = oisterFile.getElementsByTagName("authors").length;
 }
 
 //only keep going if author information is not blank or undefined            
 if(checkSiteAuthors!=undefined && checkSiteAuthors!="" && checkSiteAuthors > 0 ){
	
	//set the authors email value to blank so that it doesn't contain previously defined value
	var checkSiteAuthorsEmail="";
	
	//check to see if an authorEmail element is defined, get it's value
	if(oisterFile.getElementsByTagName("authors")[0].getElementsByTagName("authorEmail")!=undefined){
	 checkSiteAuthorsEmail = oisterFile.getElementsByTagName("authors")[0].getElementsByTagName("authorEmail").length;
	}
	
	//set the author name value to blank so that it doesn't contain a previously defined value
	var checkSiteAuthorsName="";
	
	//check to see if the authorName element is defined, get it's value
	if(oisterFile.getElementsByTagName("authors")[0].getElementsByTagName("authorName")){
	 checkSiteAuthorsName = oisterFile.getElementsByTagName("authors")[0].getElementsByTagName("authorName").length;
	}
	
	//If there is an e-mail defined
  if(checkSiteAuthorsEmail > 0 && checkSiteAuthorsName > 0){
	
	 //Set up an end value for the loop counter
   var endCount = (checkSiteAuthorsName + checkSiteAuthorsEmail) * 2;  
  
   /* Since the array index of getElementsByTagName starts at 0,
    * we only want this to increase to 0 when the first authorName node is found */
	 var authorsNameCounter=-1; 
	 	 
	 /* Start a loop to read all the authorsName elements and check to see if any of them 
	  * have an authorsEmail element associated */
	 for(var i = 0; i < endCount; i++){

    //the authorsEmail node should be a child of the "authors" node
		var authorsEmailNode = oisterFile.getElementsByTagName("authors")[0].childNodes[i+1]; 
		
		 if(authorsEmailNode.nodeName == "authorName"){
		     
		 //an authorsName element was found, so increase the corresponding counter  
		 authorsNameCounter++;  
		
		 }
		 
     if(authorsEmailNode.nodeName =="authorEmail"){ //since the authorEmailNode value might occassionally actually be an authorName node, check
		     
		  //If it is an authorEmail node, the grab the value
	    if(authorsEmailNode.firstChild != null && authorsEmailNode.firstChild != undefined){
	         
	     var authorsEmail = authorsEmailNode.firstChild.nodeValue;
	    
		   //Call the writeAuthors function to create the appropriate div
		   writeAuthorsEmail(authorsEmail,authorsNameCounter);
   	   
   	  }
	   }
	 }
      	  
  }	

 }

}


function writeAuthorsEmail(authorsEmail,authorsEmailNum){
 
 //used for placement of the node, since the authorsX node should already exist 
 var authorsNameId = "authors"+authorsEmailNum;  
 
 //manipulate the DOM and create a layer to container the author's e-mail
 divAuthorsEmail = document.createElement("div");
 document.getElementById(authorsNameId).appendChild(divAuthorsEmail);
 authorsEmailId = divAuthorsEmail.id = "authorsEmail"+authorsEmailNum;
 divAuthorsEmail.className = "authorsEmail";
 document.getElementById(authorsEmailId).innerHTML = authorsEmail;
  
}